package com.zrwl.poscloud.commons.utils;

import org.junit.Test;

/***
 * 
 * @author 十六进制和ASCII码互转
 * 
 *
 */
public class HextoASCIIUtil {


	public static String convertAsciiToHex(String str,int length) {

		char[] chars = str.toCharArray();

		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			String result="00000000000000000"+Integer.toHexString((int) chars[i]);
			hex.append(result.substring(result.length()-length*2));
		}

		return hex.toString();
	}

	public static String convertAsciiToHex(String str) {

		char[] chars = str.toCharArray();

		StringBuffer hex = new StringBuffer();
		for (int i = 0; i < chars.length; i++) {
			String result="00"+Integer.toHexString((int) chars[i]);
			hex.append(result.substring(result.length()-2));
		}

		return hex.toString();
	}
	public static String convertHexToAscii(String hex) {

		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();

		// 49204c6f7665204a617661 split into two characters 49, 20, 4c...
		for (int i = 0; i < hex.length() - 1; i += 2) {

			// grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			// convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			// convert the decimal to character
			sb.append((char) decimal);

			temp.append(decimal);
		}

		return sb.toString();
	}

	/**
	 * Ascii转换为字符串
	 *
	 * @param
	 * @return
	 */

	public static String asciiToString(String value) {
		StringBuffer sbu = new StringBuffer();
		String[] chars = value.split("");
		for (int i = 0; i < chars.length; i++) {
			sbu.append((char) Integer.parseInt(chars[i] + chars[++i], 16));
		}
		return sbu.toString();
	}

	//字符串转ASCII
	public static String str2Hex(String str) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			// 这里的第二个参数16表示十六进制
			// sb.append(Integer.toString(c, 16));
			// 或用toHexString方法直接转成16进制
			sb.append(Integer.toHexString(c));
		}
		return sb.toString();
	}

	//字符前面补0
	public static String add0ForNum(String str, int strLength) {
		int strLen = str.length();
		if (strLen < strLength) {
			while (strLen < strLength) {
				StringBuffer sb = new StringBuffer();
				sb.append("0").append(str);// 左补0
//				sb.append(str).append("0");//右补0
				str = sb.toString();
				strLen = str.length();
			}
		}
		return str;
	}

	public static String getChinese(String word, int size) {
		int x20 = size - word.length();
		String start = "";
		for (int i = 0; i < x20; i++)
			start += "2020";
		return start + convertAsciiToHex(word, 2);
	}

	public static String getNumStr(int NO) {
		String Num = "0000" + Long.toHexString(NO).toUpperCase();
		return Num.substring(Num.length() - 4);//接受者 00 3d
	}

	public static String getNumStr(int NO, int length) {
		String Num = Long.toHexString(NO).toUpperCase();
		for (int i = 0; i < length; i++) {
			Num = "0" + Num;
		}
		return Num.substring(Num.length() - length);
	}

	@Test
	public void test(){
		System.out.println(convertAsciiToHex("闻喜农商银行",2));
		//95fb 559c 519c 5546 94f6 884c
		System.out.println(convertAsciiToHex("23011647134",1));
		// 3233303131363437313334
		System.out.println(convertHexToAscii("3233303131363437313334"));
		
	}
	
}
